package com.adou.multi.multi_tenancy.async;

import org.springframework.core.task.TaskDecorator;

import com.adou.multi.multi_tenancy.util.TeanantContext;

import lombok.NonNull;
/**
 * 
 * @author ADou
 *
 */
public class TenantAwareTaskDecorator implements TaskDecorator {

	@Override
	@NonNull
	public Runnable decorate(@NonNull Runnable runnable) {
		String tenantId = TeanantContext.getTenantId();
		return ()->{
			try{
				TeanantContext.setTenantId(tenantId);
				runnable.run();
			}finally {
				TeanantContext.setTenantId(null);
			}
		};
	}

}
